package com.briup.service.Impl;

import com.briup.bean.Employee;
import com.briup.bean.EmployeeExample;
import com.briup.dao.EmployeeMapper;
import com.briup.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wyq
 * @create 2021-09-22 20:26
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * Description: TODO  根据id删除员工
     *
     * @author: wyq
     * @date: 2021/9/23 21:46
     * @Param id: 员工id
     * @return: int
     */

    public int deleteById(int id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    /**
     * Description: TODO 保存或更新员工
     *
     * @author: wyq
     * @date: 2021/9/23 21:47
     * @Param employee:
     * @return: int
     */

    public int saveOrUpdate(Employee employee) {
        if (employee.getEmpId() != null) {
            return employeeMapper.updateByPrimaryKeySelective(employee);
        }
        return employeeMapper.insertSelective(employee);
    }

    /**
     * Description: TODO  查询所有员工,分页查询
     *
     * @author: wyq
     * @date: 2021/9/23 21:47
     * @return: java.util.List<com.briup.bean.Employee>
     */
    public List<Employee> queryAll() {
        return employeeMapper.selectByExampleWithDept(null);
    }

    /**
     * Description: TODO  根据id查询员工
     *
     * @author: wyq
     * @date: 2021/9/23 21:49
     * @Param id:
     * @return: com.briup.bean.Employee
     */
    public Employee queryById(int id) {
        return employeeMapper.selectByPrimaryKeyWithDept(id);
    }

    /**
     * Description: TODO 查询员工数量
     *
     * @author: wyq
     * @date: 2021/9/23 21:50
     * @return: long
     */
    public long queryCount() {
        return employeeMapper.countByExample(null);
    }

    /**
     * Description: TODO 根据员工姓名查询员工信息
     *
     * @author: wyq
     * @date: 2021/10/4 9:47
     * @Param name: 员工姓名
     * @return: java.util.List<com.briup.bean.Employee>
     */
    public List<Employee> queryByName(String name) {
        EmployeeExample example = new EmployeeExample();
        if (!StringUtils.isEmpty(name)) {
            example.createCriteria().andEmpNameEqualTo(name);
        }
        return employeeMapper.selectByExampleWithDept(example);
    }

    /**
     * Description: TODO 批量删除
     *
     * @author: wyq
     * @date: 2021/10/4 15:48
     * @Param list:  要删除id集合
     * @return: void
     */

    public void deleteBatch(ArrayList<Integer> list) {
        EmployeeExample example = new EmployeeExample();
        example.createCriteria().andEmpIdIn(list);
        employeeMapper.deleteByExample(example);
    }
}
